package dao;

import java.util.ArrayList;
import java.util.Iterator;

import model.Calendar;
import util.Utilities;

public class Linker
{
	public static ArrayList<model.LectureRoom> lectureRoomList;
	public static ArrayList<model.Group> groupList;
	public static ArrayList<model.Teacher> teacherList;
	
	public static ArrayList<model.LectureRoom> lectureRoomRAWList;	
	public static ArrayList<model.Group> groupRAWList;	
	public static ArrayList<model.Teacher> teacherRAWList;
	public static ArrayList<model.Warning> warningRAWList;
	
	public static String path = "C:/";
	
	public static MainDataAccessObject mainDataAccessObject;
	
	private static boolean sort = true;
	private static boolean makeUpdate = false;
	private static String updateDate;
	
	public static void init()
	{
		makeUpdate = true;
		updateDate = Utilities.getDateTime("yyyy-MM-dd HH:mm:ss");
		System.out.println("Linker - start init");
		
		lectureRoomList = new ArrayList<model.LectureRoom>();
		groupList = new ArrayList<model.Group>();
		teacherList = new ArrayList<model.Teacher>();
		
		lectureRoomRAWList = new ArrayList<model.LectureRoom>();	
		groupRAWList = new ArrayList<model.Group>();	
		teacherRAWList = new ArrayList<model.Teacher>();
		warningRAWList = new ArrayList<model.Warning>();
		
		mainDataAccessObject = new MainDataAccessObject();
		
		sort = true;
		
		sort();
		
		System.out.println("Linker - end init");
		makeUpdate = false;
	}
	
	public static boolean isMakeUpdate()
	{
		return makeUpdate;
	}
	
	public static String getUpdateDate()
	{
		return updateDate;
	}
	
	public static void sort()
	{
		if(sort)
		{
			sort = false;
			
			for (int i = 0; i < lectureRoomList.size(); i++)
			{
				String link = lectureRoomList.get(i).getLink();
				String id = lectureRoomList.get(i).getId();
				
				for (model.LectureRoom lrRAW : lectureRoomRAWList)
				{
					if(lrRAW.getLink().equals(link))
					{
						lrRAW.setId(id);
					}
				}
			}
			
			for (int i = 0; i < groupList.size(); i++)
			{
				String link = groupList.get(i).getLink();
				String id = groupList.get(i).getId();
				
				for (model.Group gRAW : groupRAWList)
				{
					if(gRAW.getLink().equals(link))
					{
						gRAW.setId(id);
					}
				}
			}
			
			for (int i = 0; i < teacherList.size(); i++)
			{
				String link = teacherList.get(i).getLink();
				String id = teacherList.get(i).getId();
				
				for (model.Teacher tRAW : teacherRAWList)
				{
					if(tRAW.getLink().equals(link))
					{
						tRAW.setId(id);
					}
				}
			}
			
			for (int i = 0; i < warningRAWList.size(); i++)
			{
				String info = warningRAWList.get(i).getInfo().split("/")[0].trim();
				Iterator<Calendar> calendar = mainDataAccessObject.getCalendars().values().iterator();
				while(calendar.hasNext())
				{
					Calendar c = calendar.next();
					String cName = c.getName().split("-")[0].replace("/", "").trim();
					if(cName.equals(info))
						warningRAWList.get(i).setId(c.getId());
				}
			}
			
			mainDataAccessObject.serialize();
		}
	}
}
